package com.gis.visualization;

import org.hibernate.validator.constraints.LuhnCheck;

import java.math.BigInteger;

public class LeetcodeDemo {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);
        ListNode l2 = new ListNode(2);
        l2.next = new ListNode(1);

        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
//        进位值 carry newVal 位值
        int carry = 0, newVal = 0;
//        carry>0条件 最后一位相加的进位
        while (l1 != null || l2 != null || carry > 0) {
//            newVal = l1.val + l2.val;
            newVal = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
            carry = newVal / 10;
            newVal %= 10;
            p.next = new ListNode(newVal);
            p = p.next;
            l1 = l1==null?null:l1.next;
            l2 = l2==null?null:l2.next;
        }
        System.out.println(getVal(dummy.next));
    }

    static String getVal(ListNode node) {
        String val = String.valueOf(node.val);
        if (node.next != null) {
            val = val + getVal(node.next);
        }
        return val;
    }

    static ListNode addTwoNumber(ListNode l1, ListNode l2) {
        ListNode node1 = new ListNode(1);
//        ListNode node2 = new ListNode(2);
//        ListNode node3 = new ListNode(3);
//        node1.next = node2;
//        node2.next= node3;

//        ListNode[] testNOdes = new ListNode[10];


        String val1 = getVal(node1);
        String val2 = getVal(node1);
        val1 = "1111111111111111111111111111111111111111111111";
        val2 = "1111111111111111111111111111111111111111111111";
//        int strVal1 = Integer.parseInt(new StringBuilder(val1).reverse().toString());
//        int strVal2 = Integer.parseInt(new StringBuilder(val2).reverse().toString());
//        int result = strVal1 + strVal2;

//        Long strVal1 = Long.parseLong(new StringBuilder(val1).reverse().toString());
//        Long strVal2 = Long.parseLong(new StringBuilder(val2).reverse().toString());
//        Long result = strVal1 + strVal2;
        BigInteger strVal1 = new BigInteger(new StringBuilder(val1).reverse().toString());
        BigInteger strVal2 = new BigInteger(new StringBuilder(val2).reverse().toString());
        BigInteger result = strVal1.add(strVal2);

        char[] chars = new StringBuilder(String.valueOf(result)).reverse().toString().toCharArray();
        ListNode[] nodes = new ListNode[chars.length];
//        ListNode node = new ListNode();
        for (int i = 0; i < chars.length; i++) {
            nodes[i] = new ListNode(Character.getNumericValue(chars[i]));
            int a = i;
            if (a > 0) {
                nodes[--a].next = nodes[i];
            }
        }

//        for (int i = 0; i < nodes.length -1; i++) {
//                int a = i + 1;
//                nodes[i].next = nodes[a];
//        }
        System.out.println(getVal(nodes[0]));
        return nodes[0];
    }
}
